installfile=mongodb-linux-x86_64-suse11-3.4.7.tgz

source template.sh

echo "install from $installfile to $WORK_DIR"

echo "get install files.."
tar -xzvf $installfile


if [ -d $WORK_DIR ]; then
  echo "clear $WORK_DIR"
  rm -r $WORK_DIR
fi


echo "make dirs.."

mkdir -p $WORK_DIR/bin
mkdir -p $WORK_DIR/conf
mkdir -p $WORK_DIR/keyfile
mkdir -p $WORK_DIR/nodes/config/n1/data
mkdir -p $WORK_DIR/nodes/config/n2/data
mkdir -p $WORK_DIR/nodes/config/n3/data

mkdir -p $WORK_DIR/nodes/shard1/n1/data
mkdir -p $WORK_DIR/nodes/shard1/n2/data
mkdir -p $WORK_DIR/nodes/shard1/n3/data

mkdir -p $WORK_DIR/nodes/shard2/n1/data
mkdir -p $WORK_DIR/nodes/shard2/n2/data
mkdir -p $WORK_DIR/nodes/shard2/n3/data

mkdir -p $WORK_DIR/nodes/mongos/n1
mkdir -p $WORK_DIR/nodes/mongos/n2
mkdir -p $WORK_DIR/nodes/mongos/n3

cp ${installfile//.tgz//}/bin/* $WORK_DIR/bin

echo "generate config file.."

echo "$CONF_NODE_STR" > $CONF_NODE
echo "$CONF_ROUTER_STR" > $CONF_ROUTER

echo "generate key file.."

openssl rand -base64 756 > mongo.key
chmod 400 mongo.key
mv mongo.key $WORK_DIR/keyfile


CMD_START_CONFIG="
echo 'start configs..'

$MONGOD --port $PORT_CONFIG_1 --configsvr --replSet configReplSet --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/config/n1/data --pidfilepath $WORK_DIR/nodes/config/n1/db.pid --logpath $WORK_DIR/nodes/config/n1/db.log --config $CONF_NODE

$MONGOD --port $PORT_CONFIG_2 --configsvr --replSet configReplSet --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/config/n2/data --pidfilepath $WORK_DIR/nodes/config/n2/db.pid --logpath $WORK_DIR/nodes/config/n2/db.log --config $CONF_NODE

$MONGOD --port $PORT_CONFIG_3 --configsvr --replSet configReplSet --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/config/n3/data --pidfilepath $WORK_DIR/nodes/config/n3/db.pid --logpath $WORK_DIR/nodes/config/n3/db.log --config $CONF_NODE
"
CMD_START_SHARD1="
echo 'start shard1...'

$MONGOD --port $PORT_SHARD1_1 --shardsvr --replSet shard1 --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/shard1/n1/data --pidfilepath $WORK_DIR/nodes/shard1/n1/db.pid --logpath $WORK_DIR/nodes/shard1/n1/db.log --config $CONF_NODE
$MONGOD --port $PORT_SHARD1_2 --shardsvr --replSet shard1 --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/shard1/n2/data --pidfilepath $WORK_DIR/nodes/shard1/n2/db.pid --logpath $WORK_DIR/nodes/shard1/n2/db.log --config $CONF_NODE
$MONGOD --port $PORT_SHARD1_3 --shardsvr --replSet shard1 --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/shard1/n3/data --pidfilepath $WORK_DIR/nodes/shard1/n3/db.pid --logpath $WORK_DIR/nodes/shard1/n3/db.log --config $CONF_NODE
"
CMD_START_SHARD2="
echo 'start shard2...'

$MONGOD --port $PORT_SHARD2_1 --shardsvr --replSet shard2 --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/shard2/n1/data --pidfilepath $WORK_DIR/nodes/shard2/n1/db.pid --logpath $WORK_DIR/nodes/shard2/n1/db.log --config $CONF_NODE
$MONGOD --port $PORT_SHARD2_2 --shardsvr --replSet shard2 --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/shard2/n2/data --pidfilepath $WORK_DIR/nodes/shard2/n2/db.pid --logpath $WORK_DIR/nodes/shard2/n2/db.log --config $CONF_NODE
$MONGOD --port $PORT_SHARD2_3 --shardsvr --replSet shard2 --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/shard2/n3/data --pidfilepath $WORK_DIR/nodes/shard2/n3/db.pid --logpath $WORK_DIR/nodes/shard2/n3/db.log --config $CONF_NODE
"
CMD_START_MONGOS="
echo 'start mongos...'

$MONGOS --port=$PORT_MONGOS_1 --configdb configReplSet/127.0.0.1:$PORT_CONFIG_1,127.0.0.1:$PORT_CONFIG_2,127.0.0.1:$PORT_CONFIG_3 --keyFile $KEYFILE --pidfilepath $WORK_DIR/nodes/mongos/n1/db.pid --logpath $WORK_DIR/nodes/mongos/n1/db.log --config $CONF_ROUTER
$MONGOS --port $PORT_MONGOS_2 --configdb configReplSet/127.0.0.1:$PORT_CONFIG_1,127.0.0.1:$PORT_CONFIG_2,127.0.0.1:$PORT_CONFIG_3 --keyFile $KEYFILE --pidfilepath $WORK_DIR/nodes/mongos/n2/db.pid --logpath $WORK_DIR/nodes/mongos/n2/db.log --config $CONF_ROUTER
$MONGOS --port $PORT_MONGOS_3 --configdb configReplSet/127.0.0.1:$PORT_CONFIG_1,127.0.0.1:$PORT_CONFIG_2,127.0.0.1:$PORT_CONFIG_3 --keyFile $KEYFILE --pidfilepath $WORK_DIR/nodes/mongos/n3/db.pid --logpath $WORK_DIR/nodes/mongos/n3/db.log --config $CONF_ROUTER
"

export IFS=" "

eval $CMD_START_CONFIG
echo "initial config replset.."
$MONGO --port $PORT_CONFIG_1 --host 127.0.0.1 --eval "$REPL_CFG_CONFIG"

eval $CMD_START_SHARD1
echo "initial shard1 replset.."
$MONGO --port $PORT_SHARD1_1 --host 127.0.0.1 --eval "$REPL_CFG_SHARD1"

eval $CMD_START_SHARD2
echo "initial shard2 replset.."
$MONGO --port $PORT_SHARD2_1 --host 127.0.0.1 --eval "$REPL_CFG_SHARD2"

eval $CMD_START_MONGOS
echo "initial mongos.."
$MONGO --port $PORT_MONGOS_1 --host 127.0.0.1 --eval "$MONGOS_CFG"

echo "initial admin user for cluster.."
$MONGO --port $PORT_MONGOS_1 --host 127.0.0.1 --eval "$ADMIN_INIT"

# just try on all replset node
$MONGO --port $PORT_SHARD1_1 --host 127.0.0.1 --eval "$ADMIN_INIT"
$MONGO --port $PORT_SHARD1_2 --host 127.0.0.1 --eval "$ADMIN_INIT"
$MONGO --port $PORT_SHARD1_3 --host 127.0.0.1 --eval "$ADMIN_INIT"
$MONGO --port $PORT_SHARD2_1 --host 127.0.0.1 --eval "$ADMIN_INIT"
$MONGO --port $PORT_SHARD2_2 --host 127.0.0.1 --eval "$ADMIN_INIT"
$MONGO --port $PORT_SHARD2_3 --host 127.0.0.1 --eval "$ADMIN_INIT"

echo "initial appuser for mongos"
$MONGO --port $PORT_MONGOS_1 --host 127.0.0.1 --eval "$APP_INIT"


echo "generate scripts.."
ssh_start=$WORK_DIR/startCluster.sh
ssh_stop=$WORK_DIR/stopCluster.sh
ssh_test=$WORK_DIR/testCluster.sh
ssh_stat=$WORK_DIR/statCluster.sh
ssh_conn=$WORK_DIR/connCluster.sh


script_start='
source template.sh

echo "start configs..."

$MONGOD --port $PORT_CONFIG_1 --configsvr --replSet configReplSet --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/config/n1/data --pidfilepath $WORK_DIR/nodes/config/n1/db.pid --logpath $WORK_DIR/nodes/config/n1/db.log --config $CONF_NODE

$MONGOD --port $PORT_CONFIG_2 --configsvr --replSet configReplSet --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/config/n2/data --pidfilepath $WORK_DIR/nodes/config/n2/db.pid --logpath $WORK_DIR/nodes/config/n2/db.log --config $CONF_NODE

$MONGOD --port $PORT_CONFIG_3 --configsvr --replSet configReplSet --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/config/n3/data --pidfilepath $WORK_DIR/nodes/config/n3/db.pid --logpath $WORK_DIR/nodes/config/n3/db.log --config $CONF_NODE


echo "start shard1..."

$MONGOD --port $PORT_SHARD1_1 --shardsvr --replSet shard1 --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/shard1/n1/data --pidfilepath $WORK_DIR/nodes/shard1/n1/db.pid --logpath $WORK_DIR/nodes/shard1/n1/db.log --config $CONF_NODE
$MONGOD --port $PORT_SHARD1_2 --shardsvr --replSet shard1 --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/shard1/n2/data --pidfilepath $WORK_DIR/nodes/shard1/n2/db.pid --logpath $WORK_DIR/nodes/shard1/n2/db.log --config $CONF_NODE
$MONGOD --port $PORT_SHARD1_3 --shardsvr --replSet shard1 --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/shard1/n3/data --pidfilepath $WORK_DIR/nodes/shard1/n3/db.pid --logpath $WORK_DIR/nodes/shard1/n3/db.log --config $CONF_NODE

echo "start shard2..."

$MONGOD --port $PORT_SHARD2_1 --shardsvr --replSet shard2 --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/shard2/n1/data --pidfilepath $WORK_DIR/nodes/shard2/n1/db.pid --logpath $WORK_DIR/nodes/shard2/n1/db.log --config $CONF_NODE
$MONGOD --port $PORT_SHARD2_2 --shardsvr --replSet shard2 --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/shard2/n2/data --pidfilepath $WORK_DIR/nodes/shard2/n2/db.pid --logpath $WORK_DIR/nodes/shard2/n2/db.log --config $CONF_NODE
$MONGOD --port $PORT_SHARD2_3 --shardsvr --replSet shard2 --keyFile $KEYFILE --dbpath $WORK_DIR/nodes/shard2/n3/data --pidfilepath $WORK_DIR/nodes/shard2/n3/db.pid --logpath $WORK_DIR/nodes/shard2/n3/db.log --config $CONF_NODE

echo "start mongos..."

$MONGOS --port=$PORT_MONGOS_1 --configdb configReplSet/127.0.0.1:$PORT_CONFIG_1,127.0.0.1:$PORT_CONFIG_2,127.0.0.1:$PORT_CONFIG_3 --keyFile $KEYFILE --pidfilepath $WORK_DIR/nodes/mongos/n1/db.pid --logpath $WORK_DIR/nodes/mongos/n1/db.log --config $CONF_ROUTER
$MONGOS --port $PORT_MONGOS_2 --configdb configReplSet/127.0.0.1:$PORT_CONFIG_1,127.0.0.1:$PORT_CONFIG_2,127.0.0.1:$PORT_CONFIG_3 --keyFile $KEYFILE --pidfilepath $WORK_DIR/nodes/mongos/n2/db.pid --logpath $WORK_DIR/nodes/mongos/n2/db.log --config $CONF_ROUTER
$MONGOS --port $PORT_MONGOS_3 --configdb configReplSet/127.0.0.1:$PORT_CONFIG_1,127.0.0.1:$PORT_CONFIG_2,127.0.0.1:$PORT_CONFIG_3 --keyFile $KEYFILE --pidfilepath $WORK_DIR/nodes/mongos/n3/db.pid --logpath $WORK_DIR/nodes/mongos/n3/db.log --config $CONF_ROUTER
'

script_stop='
source template.sh

echo "stop mongos..."

kill -3 `cat $WORK_DIR/nodes/mongos/n1/db.pid`
kill -3 `cat $WORK_DIR/nodes/mongos/n2/db.pid`
kill -3 `cat $WORK_DIR/nodes/mongos/n3/db.pid`

echo "stop configs..."

kill -3 `cat $WORK_DIR/nodes/config/n1/db.pid`
kill -3 `cat $WORK_DIR/nodes/config/n2/db.pid`
kill -3 `cat $WORK_DIR/nodes/config/n3/db.pid`

echo "stop shard1..."

kill -3 `cat $WORK_DIR/nodes/shard1/n1/db.pid`
kill -3 `cat $WORK_DIR/nodes/shard1/n2/db.pid`
kill -3 `cat $WORK_DIR/nodes/shard1/n3/db.pid`

echo "stop shard2..."

kill -3 `cat $WORK_DIR/nodes/shard2/n1/db.pid`
kill -3 `cat $WORK_DIR/nodes/shard2/n2/db.pid`
kill -3 `cat $WORK_DIR/nodes/shard2/n3/db.pid`

'

script_test='

source template.sh

echo "test mongos..."

netstat -nlp |grep $PORT_MONGOS_1
netstat -nlp |grep $PORT_MONGOS_2
netstat -nlp |grep $PORT_MONGOS_3

echo "test configs..."

netstat -nlp |grep $PORT_CONFIG_1
netstat -nlp |grep $PORT_CONFIG_2
netstat -nlp |grep $PORT_CONFIG_3

echo "test shard1..."

netstat -nlp |grep $PORT_SHARD1_1
netstat -nlp |grep $PORT_SHARD1_2
netstat -nlp |grep $PORT_SHARD1_3

echo "test shard2..."

netstat -nlp |grep $PORT_SHARD2_1
netstat -nlp |grep $PORT_SHARD2_2
netstat -nlp |grep $PORT_SHARD2_3
'

script_stat='
source template.sh
$WORK_DIR/bin/mongostat --port $PORT_MONGOS_1 --host 127.0.0.1 -u $ADMIN_USER -p $ADMIN_PWD --authenticationDatabase=$ADMIN_DB --discover
'

script_conn='
source template.sh
$WORK_DIR/bin/mongo --port $PORT_MONGOS_1 --host 127.0.0.1 -u $ADMIN_USER -p $ADMIN_PWD --authenticationDatabase=$ADMIN_DB 
'

cp template.sh $WORK_DIR
echo $script_start > $ssh_start
echo $script_stop > $ssh_stop
echo $script_test > $ssh_test
echo $script_stat > $ssh_stat
echo $script_conn > $ssh_conn

cd $WORK_DIR
chmod +x *.sh
$ssh_stat